package com.example.demo.friendship.controller;

import com.example.demo.common.utils.ResultUtil;
import com.example.demo.common.vo.Result;
import com.example.demo.friendship.dao.mongdb.FriendRelationDao;
import com.example.demo.friendship.model.mongodb.FriendRelation;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class FriendController {

    @Autowired
    private FriendRelationDao friendRelationDao;

    @RequestMapping(value = "/FriendRelation/save",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "保存数据")
    public Result<FriendRelation> save(@RequestBody FriendRelation entity){
        List<FriendRelation> friendRelations = friendRelationDao.findByFriendLeft_FbIdAndAndFriendRight_FbId(entity.getFriendLeft().getFbId(),entity.getFriendRight().getFbId());
        List<FriendRelation> friendRelations_reverse = friendRelationDao.findByFriendLeft_FbIdAndAndFriendRight_FbId(entity.getFriendRight().getFbId(),entity.getFriendLeft().getFbId());
        if(friendRelations.size() > 0 || friendRelations_reverse.size() > 0){
            System.out.println("已经存在");
        } else {
            FriendRelation e = friendRelationDao.save(entity);
            return new ResultUtil<FriendRelation>().setData(e);
        }
        return null;
    }

}
